
// import { WechatPlugin } from 'vux'
import http from '../APIService/http'
import Cookie from '../utils/auth'
import VueLazyload from 'vue-lazyload'
const wxApi = {
  install: async function (Vue) { // 核心部分，在我们使用Vue.use()时，自动调用的是install，而install导出的必须是的组件
    // Vue.use(WechatPlugin)
    let prefix = ''
    if (process.env.NODE_ENV === 'production') {
      prefix = '/api/'
    } else {
      prefix = 'http://127.0.0.1:3000/'
    }
    Vue.prototype.$prefix = prefix
    Vue.prototype.$Cookie = Cookie
    Vue.prototype.$http = http
    Vue.prototype.$isWx = false
    Vue.use(VueLazyload, {
      preLoad: 1.3,
      loading: require('../assets/imgs/lazy.png'),
      error: require('../assets/imgs/error.png'),
      attempt: 1
    })
    if (getIsWxClient()) {
      Vue.prototype.$isWx = true
      let obj = await getSignature()
      obj.debug = false
      obj.jsApiList = [
      // 所有要调用的 API 都要加到这个列表中
        'chooseImage',
        'updateAppMessageShareData',
        'updateTimelineShareData',
        'onMenuShareWeibo',
        'onMenuShareAppMessage'
      ]
      wx.config(obj)
    }
  }
}

/**
     * 判断是否是微信环境
     */
function getIsWxClient () {
  let ua = navigator.userAgent.toLowerCase()
  if (String(ua.match(/MicroMessenger/i)) === 'micromessenger') {
    return true
  }
  return false
};
async function getSignature () {
  try {
    let params = {
      targetUrl: encodeURIComponent(window.location.href.split('#')[0])
      // targetUrl: encodeURIComponent('http://yijie123.free.idcfengye.com/detail')
    }
    let res = await http.get(`/wx/signature`, params)
    return res.data
  } catch (err) {
    console.log('getSignature fail:', err)
  }
}

export default wxApi
